package com.keon.jianbao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.keon.jianbao.adapter.TweetListAdapter;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseListAdapter;
import com.keon.jianbao.base.BaseListFragment;
import com.keon.jianbao.bean.ListEntity;
import com.keon.jianbao.bean.Tweet;
import com.keon.jianbao.bean.TweetList;
import com.keon.jianbao.ui.activity.TweetDetailActivity;

/**
 * Created by keon on 2015/9/2.
 */
public class TweetListFragment extends BaseListFragment {

    int type = 2;
    int size = 10;
    int user_id = 0;

    @Override
    protected void sendRequestData() {

        Bundle bundle = getArguments();
        type = bundle.getInt("type");
        mCurrentPage = bundle.getInt("page");

        if (type == 3) {
            user_id = bundle.getInt("user_id");
            Api.get_tidings(type, mCurrentPage, size, user_id, "", mHandler);
        } else {
            String selectedCatlog = bundle.getString("selectedCatlog");
            Api.get_tidings(type, mCurrentPage, size, selectedCatlog, mHandler);
        }

    }

    @Override
    protected ListEntity parseList(String reponseData) {

        TweetList tweetList = new TweetList(reponseData);

        return tweetList;
    }

    @Override
    protected BaseListAdapter getListAdapter() {
        return new TweetListAdapter(getActivity());
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected String getCacheKeyPrefix() {
        return "TWEETLIST";
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
//        super.onItemClick(parent, view, position, id);
        if (position < mAdapter.getCount() - 1) {
            BaseActivity ba = (BaseActivity) getActivity();
            Bundle bundle = new Bundle();
            Tweet tweet = (Tweet) mAdapter.getData().get(position);
            bundle.putInt("id", tweet.getId());
            bundle.putSerializable("tweet", tweet);
            ba.jump(TweetDetailActivity.class, bundle);
        }
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        mListView.setDividerHeight(10);
    }
}
